package cn.operation.api.rest;

import cn.operation.api.query.baseArea.AreaIdAndCodeQuery;
import cn.operation.api.query.baseArea.IdOrCodeQuery;
import cn.operation.api.query.baseArea.IdOrNameOrCodeQuery;
import cn.operation.backend.bo.baseArea.BaseAreaBo;
import cn.operation.backend.bo.baseArea.BaseParentAreaBo;
import cn.operation.backend.enums.BaseAreaLevelEnum;
import cn.operation.backend.enums.StatusEnum;
import cn.operation.backend.service.baseArea.BaseAreaService;
import cn.operation.backend.util.BusinessException;
import cn.operation.backend.util.PageVo;
import cn.operation.backend.util.ResultDTO;
import cn.operation.backend.util.ResultDTOWithPagination;
import cn.operation.base.model.ScBaseArea;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @FileName:
 * @Description: 区域数据
 * @author: Felix
 * @date: 2017/5/24
 */

@Controller
@RequestMapping(value = "/api/area", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/area", description = "区域信息")
public class ScBaseAreaRest {

	@Autowired
	private BaseAreaService baseAreaService;
	private static final Logger LOGGER = LoggerFactory.getLogger(ScBaseAreaRest.class);

	@ApiIgnore
	@ResponseBody
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ApiOperation(value = "根据区域ID查询区域信息", notes = "根据区域ID查询区域信息", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid id supplied"),
			@ApiResponse(code = 404, message = "area not found")})
	public ResultDTO<Object> getById(@ApiParam(value = "区域ID", required = true) @PathVariable("id") Integer id)
			throws Exception {
		LOGGER.info("ScBaseAreaRest.getById param: {}", id);
		ResultDTO<Object> result = new ResultDTO<Object>(true, null, "查询成功！");
		try {
			BaseAreaBo baseAreaBo = baseAreaService.selectById(id, StatusEnum.NORMAL.getValue());
			result.setData(baseAreaBo);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getById error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ApiIgnore
	@ResponseBody
	@RequestMapping(value = "/getChildAreaList", method = RequestMethod.GET)
	@ApiOperation(value = "根据区域查询子级区域", notes = "根据区域查询子级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid id supplied"),
			@ApiResponse(code = 404, message = "childArea not found")})
	public ResultDTOWithPagination<Object> getChildAreaList(
			@ApiParam(value = "区域ID或编码 ", required = true) IdOrCodeQuery condition,
			PageVo pageVo)
			throws Exception {
		LOGGER.info("ScBaseAreaRest.getChildAreaList param: {}, {}", condition, pageVo);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！", pageVo);
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(condition, baseAreaBo);
			List<ScBaseArea> list = baseAreaService.getChildAreaListByIdOrCode(pageVo, baseAreaBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.v error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getAllProvince", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有省级区域 ", notes = "查询所有省级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 404, message = "getAllProvinceArea not found")})
	public ResultDTOWithPagination<Object> getAllProvinceArea(@ApiParam(required = false, value = "分页参数") PageVo pageVo,
															  @ApiParam(required = false, value = "查询参数") IdOrNameOrCodeQuery condition) throws Exception {
		LOGGER.info("ScBaseAreaRest.getAllProvinceArea param: {}");
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！", pageVo);
		try {
			pageVo.setPageSize(100);
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(condition, baseAreaBo);
			baseAreaBo.setLevel(BaseAreaLevelEnum.PROVINCE.getValue());
			List<ScBaseArea> list = baseAreaService.selectAreaListByConditions(pageVo, baseAreaBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getAllProvinceArea error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getAllCity", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有市级区域 ", notes = "查询所有市级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 404, message = "getAllCityArea not found")})
	public ResultDTOWithPagination<Object> getAllCityArea(@ApiParam(required = false, value = "分页参数") PageVo pageVo,
														  @ApiParam(required = false, value = "查询参数") IdOrNameOrCodeQuery condition) throws Exception {
		LOGGER.info("ScBaseAreaRest.getAllCityArea param: {}");
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<Object>(true, null, "查询成功！", pageVo);
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(condition, baseAreaBo);
			baseAreaBo.setLevel(BaseAreaLevelEnum.CITY.getValue());
			List<ScBaseArea> list = baseAreaService.selectAreaListByConditions(pageVo, baseAreaBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getAllCityArea error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getAllCounty", method = RequestMethod.GET)
	@ApiOperation(value = "查询所有市级区域 ", notes = "查询所有市级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 404, message = "getAllCounty not found")})
	public ResultDTOWithPagination<Object> getAllCounty(@ApiParam(required = false, value = "分页参数") PageVo pageVo,
														@ApiParam(required = false, value = "查询参数") IdOrNameOrCodeQuery condition) throws Exception {
		LOGGER.info("ScBaseAreaRest.getAllCounty param: {}");
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<Object>(true, null, "查询成功！", pageVo);
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(condition, baseAreaBo);
			baseAreaBo.setLevel(BaseAreaLevelEnum.COUNTY.getValue());
			List<ScBaseArea> list = baseAreaService.selectAreaListByConditions(pageVo, baseAreaBo);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getAllCounty error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getCityAndCounty", method = RequestMethod.GET)
	@ApiOperation(value = "根据省级区域查询其下市级区域和县级区域", notes = "查询省级区域查询其下市级区域和县级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid id supplied"),
			@ApiResponse(code = 404, message = "provinceAndCityAreaList not found")})
	public ResultDTOWithPagination<Object> getCityAndCounty(PageVo pageVo,
															@ApiParam(value = "区域ID或编码 ", required = true) IdOrCodeQuery condition) throws Exception {
		LOGGER.info("ScBaseAreaRest.getCityAndCounty param: {}, {}", pageVo, condition);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<Object>(true, null, "查询成功！", pageVo);
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(condition, baseAreaBo);
			List<ScBaseArea> list = baseAreaService.getCityAndCountyAreaList(pageVo, baseAreaBo);

			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getCityAndCounty error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/getByIdOrCode", method = RequestMethod.POST)
	@ApiOperation(value = "根据区域ID或者编码查询区域信息", notes = "根据区域ID或者编码查询区域信息")
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid id or code supplied"),
			@ApiResponse(code = 404, message = "area not found")})
	public ResultDTO<Object> getByIdOrCode(@ApiParam(value = "区域ID或编码", required = true) AreaIdAndCodeQuery param)
			throws Exception {
		LOGGER.info("ScBaseAreaRest.getByIdOrCode param: {}", param);
		ResultDTO<Object> result = new ResultDTO<Object>(true, null, "查询成功！");
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(param, baseAreaBo);
			List<ScBaseArea> areaBoList = baseAreaService.selectAreByIdOrCode(baseAreaBo);
			if (areaBoList != null && areaBoList.size() > 0) {
				result.setData(areaBoList.get(0));
			}
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getByIdOrCode error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

    /*@ResponseBody
	@RequestMapping(value = "/getAreaTree", method = RequestMethod.POST)
    @ApiOperation(value = "根据区域ID或者编码递归查询下级区域信息", notes = "根据区域ID或者编码递归查询下级区域信息")
    @ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid id or code supplied"),
            @ApiResponse(code = 404, message = "area not found") })
    public ResultDTO<Object> getAreaTree(@ApiParam(value = "区域ID或编码", required = true) AreaIdAndCodeQuery param)
            throws Exception {
        LOGGER.info("ScBaseAreaRest.getAreaTree param: {}", param);
        ResultDTO<Object> result = new ResultDTO<Object>(true, null, "查询成功！");
        try {
            BaseAreaBo baseAreaBo = new BaseAreaBo();
            BeanUtils.copyProperties(param, baseAreaBo);
            List<ScBaseAreaDto> areaBoList = baseAreaService.selectTreeByparentId(baseAreaBo);
            if (areaBoList!=null&&areaBoList.size()>0) {
                result.setData(areaBoList.get(0));
            }
        } catch (Exception e) {
            LOGGER.error("ScBaseAreaRest.getAreaTree error: {}", e);
            result.setMessage(e.getMessage());
            result.setSuccess(false);
        }
        return result;
    }*/

	@ResponseBody
	@RequestMapping(value = "/getParenArea", method = RequestMethod.POST)
	@ApiOperation(value = "根据区域ID或者编码递归查询上级区域信息", notes = "根据区域ID或者编码递归查询上级区域信息")
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid id or code supplied"),
			@ApiResponse(code = 404, message = "area not found")})
	public ResultDTO<Object> getParenArea(@ApiParam(value = "区域ID或编码", required = true) AreaIdAndCodeQuery param)
			throws Exception {
		LOGGER.info("ScBaseAreaRest.getParenArea param: {}", param);
		ResultDTO<Object> result = new ResultDTO<Object>(true, null, "查询成功！");
		try {
			BaseAreaBo baseAreaBo = new BaseAreaBo();
			BeanUtils.copyProperties(param, baseAreaBo);
			BaseParentAreaBo area = baseAreaService.getParenAreaList(baseAreaBo);
			result.setData(area);
		} catch (Exception e) {
			LOGGER.error("ScBaseAreaRest.getParenArea error: {}", e);
			result.setMessage(e.getMessage());
			result.setSuccess(false);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "/queryCityByPid/{paraentId}", method = RequestMethod.GET)
	@ApiOperation(value = "查询渠道线路省级下面的市级区域", notes = "查询渠道线路省级下面的市级区域", produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiResponses(value = {@ApiResponse(code = 400, message = "Invalid Code supplied"),
			@ApiResponse(code = 404, message = "data not found")})
	public ResultDTOWithPagination<Object> queryStartCity(
			@ApiParam(value = "区域关键字") @PathVariable("paraentId") Integer paraentId) throws Exception {
		LOGGER.info("LnLineRest.queryStartCity param: {}", paraentId);
		ResultDTOWithPagination<Object> result = new ResultDTOWithPagination<>(true, null, "查询成功！");
		if (paraentId == null) {
			throw new BusinessException("查询下属区域失败");
		}
		try {
			List<ScBaseArea> list = baseAreaService.queryStartCity(paraentId);
			result.setData(list);
		} catch (Exception e) {
			LOGGER.error("LnLineRest.queryStartArea error: {}", e);
			throw new BusinessException(e.getMessage());
		}
		return result;
	}
}
